package 优先算法.滑动窗口.将x减到0的最小操作数;

public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {5,6,7,8,9};
        int target = 4 ;
        int rel = so.minOperations(arr, target);
        System.out.println(rel);
    }
}
class Solution {
    public int minOperations(int[] nums, int x) {
        int all = 0;
        for(int i : nums){
            all+=i;
        }
        int sum = 0,rel=-1,left = 0,right =0,target = (all - x);
        while(right < nums.length){
            //进窗口
            sum += nums[right++];

            while(left < nums.length && sum > target){
                sum -= nums[left++];
            }

            if(sum == target){
                rel=Math.max(rel,right-left);
            }
        }
        return rel == -1 ? rel : nums.length-rel;

    }
}